package com.jule.teenpatti.game.network.protocol.reqs;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.core.common.log.LoggerUtils;
import com.jule.teenpatti.base.bean.ItemBean;
import com.jule.teenpatti.base.bean.UnitVO;
import com.jule.teenpatti.base.enums.GameConst;
import com.jule.teenpatti.base.service.ItemServer;
import com.jule.teenpatti.game.config.Config;
import com.jule.teenpatti.game.network.protocol.ClientReq;
import com.jule.teenpatti.game.network.protocol.acks.JoloGame_ItemStatusAck_50054;
import com.jule.teenpatti.game.play.AbstractTable;
import com.jule.teenpatti.game.service.TableService;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class JoloGame_ItemStatusReq_50054 extends ClientReq {
    private long time;

    public JoloGame_ItemStatusReq_50054(int functionId) {
        super(functionId);
    }

    private JoloGame.JoloGame_ItemsStatusReq req;

    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        time = System.currentTimeMillis();
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
        req = JoloGame.JoloGame_ItemsStatusReq.parseFrom(blob);
        this.setTable(TableService.getInstance().getTable(header.gameId + "", req.getRoomId(), req.getTableId()));
    }

    @Override
    public void processImpl() throws Exception {
        log.debug("收到消息-> " + functionId + ", reqNum-> " + header.reqNum + ", req->" + req.toString());
        String userId = req.getUserId();
        int itemType = req.getItemType();
        String roomId = req.getRoomId();
        String tableId = req.getTableId();
        AbstractTable table = getTable();

        JoloGame.JoloGame_ItemsStatusAck.Builder ack = JoloGame.JoloGame_ItemsStatusAck.newBuilder();
        ack.setResult(1).setRoomId(roomId).setTableId(tableId).setUserId(userId);
        try {
            UnitVO unitVO = ItemServer.OBJ.getUnitByType(Config.GAME_ID, userId, itemType);
            if (unitVO.getResult() == 0) {
                List<ItemBean> list = unitVO.getItems();
                Map<Integer, JoloGame.JoloGame_ItemStatusInfo> items = new HashMap<>();
                list.forEach(itemBean -> {
                    JoloGame.JoloGame_ItemStatusInfo.Builder itemStatusInfo = JoloGame.JoloGame_ItemStatusInfo.newBuilder();
                    itemStatusInfo.setCountDown(itemBean.getTimeOut())
                            .setItemId("" + itemBean.getItemID())
                            .setNum(itemBean.getItemNum());
                    items.put(itemBean.getItemID(), itemStatusInfo.build());
                });
                ack.addAllItemStatusInfoList(items.values());
            }
            ctx.writeAndFlush(new JoloGame_ItemStatusAck_50054(ack.build(), header));
            long timeMillis = System.currentTimeMillis() - time;
            if (timeMillis > GameConst.COST_TIME) {
                LoggerUtils.performance.info("ItemStatusReq_50054,cost time:{},req:{}", timeMillis, req.toString());
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
        }
    }
}
